package com.xkh.rocketmq.service;

import lombok.EqualsAndHashCode;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.context.ApplicationEvent;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
public class RocketMQEvent extends ApplicationEvent {

    private static final long serialVersionUID = -4468405250074063206L;
    private DefaultMQPushConsumer consumer;
    private List<MessageExt> msg;

    public RocketMQEvent(List<MessageExt> msg, DefaultMQPushConsumer consumer) {
        super(msg);
        this.consumer = consumer;
        this.setMsg(msg);
    }

    public String getMsg(int idx) {
        return new String(getMsg().get(idx).getBody(), StandardCharsets.UTF_8);
    }

    public String getMsg(int idx, String code) {
        try {
            return new String(getMsg().get(idx).getBody(), code);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public List<MessageExt> getMsg() {
        return msg;
    }

    public void setMsg(List<MessageExt> msg) {
        this.msg = msg;
    }


}
